#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n , m;
    cin >> n >> m;
    map<string , string> mp;
    for(int i = 0;i < n;i ++)
    {
        string s1 , s2;
        cin >> s1 >> s2;
        mp[s1] = s2;
    }
    while(m--)
    {
        string s;
        cin >> s;
        if(mp.count(s)) cout << mp[s] << endl;
        else
        {
            vector<string>ans;
            for(int i = 0;i < s.size();i ++)
            {
                string p = s.substr(0 , i);
                string q = s.substr(i);
                if(mp.count(p) && mp.count(q)) ans.push_back(mp[p] + mp[q]);
            }
            if(ans.size() == 1) cout << ans[0] << endl;
            else cout << "D" << endl;
        }
    }
    return 0;
}